﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Windows.Forms; 


namespace CardControlLibrary
{
    public partial class CardView : UserControl
    {

        private CardModel m_CardModel;
        private CardThreadSafeOpperations m_UI_TS; 

        public CardView(CardModel cm)
        {
            InitializeComponent();
            m_CardModel = cm; 
            m_UI_TS = new CardThreadSafeOpperations();
        }

        public bool GetCollapsed()
        {
            return m_CardModel.Collapsed;
        }

        public delegate void CollapseExpandHandler(CardView cc);
        public event CollapseExpandHandler OnCollapseExpand;
        protected void OnCollapseExpandHandler(CardView cc)
        {
            if (OnCollapseExpand != null)
            {
                OnCollapseExpand(cc);
            }
        }

       
        /// <summary>
        /// Visualisation of single Card; TO-DO make dimensions flexible depends on Text size.
        /// </summary>
        
        public int MinWidth { get { return 200; } }
        public int MinHeight { get { return 25; } }
        public int MaxWidth { get { return 300; } }
        public int MaxHeight { get { return 100; } }



        public void DrawControl()
        {
            // Set data
            m_UI_TS.SetText(lbTitle, m_CardModel.Title);
            m_UI_TS.SetText(lbDescription, m_CardModel.Description);
            m_UI_TS.SetText(lbURL, m_CardModel.WebSite);
            //m_UI_TS.SetDocumentText(webBrowser, m_CardModel.Description);
           

           // webControl.Document.Window
            //HtmlDocument doc = new HtmlDocument();
            //doc.

            //Starts "drawing"
            //Graphics graphics = this.CreateGraphics();

            if (GetCollapsed())
            {
                m_UI_TS.SetWidth(this, MinWidth);
                m_UI_TS.SetHeight(this, MinHeight);
                m_UI_TS.SetImage(pictureBox2, CardControlLibrary.Properties.Resources.down_arrow);
                if (lbTitle.Text.Length > 22) m_UI_TS.SetText(lbTitle, lbTitle.Text.Substring(0, 22) + "...");
            }
            else
            {
                m_UI_TS.SetWidth(this, MaxWidth);
                m_UI_TS.SetHeight(this, MaxHeight);
                m_UI_TS.SetImage(this.pictureBox2, CardControlLibrary.Properties.Resources.up_arrow);
             }


            m_UI_TS.SetLocation(lbTitle, new System.Drawing.Point(5, 5));
            m_UI_TS.SetLocation(this.pictureBox2, new System.Drawing.Point(this.Width - 25, 3));
            m_UI_TS.SetWidth(this.pictureBox2, 20);
            m_UI_TS.SetHeight(this.pictureBox2, 20);
            
            m_UI_TS.SetLocation(this.pictureBox3, new System.Drawing.Point(this.Width - 55, 3));
            m_UI_TS.SetWidth(this.pictureBox3, 20);
            m_UI_TS.SetHeight(this.pictureBox3, 20);
            m_UI_TS.SetImage(pictureBox3, CardControlLibrary.Properties.Resources.add_icon);


            m_UI_TS.SetWidth(this.pictureBox1, 32);
            m_UI_TS.SetHeight(this.pictureBox1, 32);
            m_UI_TS.SetLocation(this.pictureBox1, new System.Drawing.Point(5, 25));
            m_UI_TS.SetLocation(this.lbDescription, new System.Drawing.Point(37, 25));
            m_UI_TS.SetWidth(lbDescription, this.Width - 42);
            
            //m_UI_TS.SetLocation(this.webBrowser, new System.Drawing.Point(37, 25));
            //m_UI_TS.SetWidth(this.webBrowser, this.Width - 42);
            
            m_UI_TS.SetLocation(lbURL, new System.Drawing.Point(5, 25 + lbDescription.Height)); 
            
        }

   
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            m_CardModel.Collapsed = !m_CardModel.Collapsed;
            //DrawControl();
            OnCollapseExpandHandler(this); 
        }

        private void lbURL_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Process.Start(((LinkLabel)sender).Text);
            }
            catch (Win32Exception ex)
            {
                //Utilities.TraceWrapper.LogException("linkLabel1_LinkClicked error: " + ex.Message, ex);

                //nothing is registered to handle URLs, so let's use IE!
                Process.Start("IExplore.exe", ((LinkLabel)sender).Text);
            } 
 
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            this.m_CardModel.AddToDocument();
        }

       
    }
}
